(*
 VPN Enabler.applescript
 VPN Enabler

 Created by Link M Dupont on 9/5/08.
 Copyright 2008 Link M Dupont. All rights reserved.

 This file is part of VPN Enabler.
 
 VPN Enabler is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, version 2 of the License.
 
 VPN Enabler is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with VPN Enabler.  If not, see <http://www.gnu.org/licenses/>.
*)

property mailName : ""
property ichatName : ""
property colloquyName : ""

on should quit after last window closed theObject
	return false
end should quit after last window closed

on will hide theObject
	(*Add your script here.*)
end will hide

on clicked theObject
	if name of theObject is "mailToggle" then
		mailToggle()
	else if name of theObject is "iChatToggle" then
		iChatToggle()
	else if name of theObject is "colloquyToggle" then
		colloquyToggle()
	end if
end clicked

on will quit theObject
	tell user defaults
		set contents of default entry "mailName" to mailName
		set contents of default entry "iChatName" to ichatName
		set contents of default entry "colloquyName" to colloquyName
	end tell
end will quit

on will show theObject
	(*Add your script here.*)
end will show

on will finish launching theObject
	tell user defaults
		make new default entry at end of default entries with properties {name:"mailName", contents:""}
		make new default entry at end of default entries with properties {name:"ichatName", contents:""}
		make new default entry at end of default entries with properties {name:"colloquyName", contents:""}
	end tell
	
	tell user defaults
		set mailName to contents of default entry "mailName"
		set ichatName to contents of default entry "ichatName"
		set colloquyName to contents of default entry "colloquyName"
	end tell
end will finish launching

on choose menu item theObject
	if name of theObject is "preferencesMenu" then
		show window "preferencesWindow"
	end if
end choose menu item

on should close theObject
	if name of theObject is "preferencesWindow" then
		tell window "preferencesWindow"
			set mailName to title of popup button "mailAccountSelect"
			set ichatName to title of popup button "iChatAccountSelect"
			set colloquyName to title of popup button "colloquyAccountSelect"
		end tell
		hide window "preferencesWindow"
		return false
	else
		hide theObject
	end if
end should close

on will open theObject
	if name of theObject is "preferencesWindow" then
		tell application "Mail" to set mailAccounts to get name of every account
		tell application "iChat" to set iChatAccounts to get name of every service
		tell application "Colloquy" to set colloquyAccounts to get server address of every connection
		tell window "preferencesWindow"
			delete every menu item of menu of popup button "mailAccountSelect"
			delete every menu item of menu of popup button "iChatAccountSelect"
			delete every menu item of menu of popup button "colloquyAccountSelect"
			repeat with each in mailAccounts
				make new menu item at the end of menu items of menu of popup button "mailAccountSelect" with properties {title:each, enabled:true}
			end repeat
			repeat with each in iChatAccounts
				make new menu item at the end of menu items of menu of popup button "iChatAccountSelect" with properties {title:each, enabled:true}
			end repeat
			repeat with each in colloquyAccounts
				make new menu item at the end of menu items of menu of popup button "colloquyAccountSelect" with properties {title:each, enabled:true}
			end repeat
			set title of popup button "mailAccountSelect" to mailName
			set title of popup button "iChatAccountSelect" to ichatName
			set title of popup button "colloquyAccountSelect" to colloquyName
		end tell
	end if
end will open

on action theObject
	(*Add your script here.*)
end action

on will close theObject
	hide theObject
end will close

on activated theObject
	-- This shows the main window when the application gains focus from *another* application
	show window "mainWindow"
end activated

on open untitled theObject
	-- This shows the main window when the application Dock icon is clicked while the application *has* focus
	show window "mainWindow"
end open untitled

on open theObject
	(*Add your script here.*)
end open

on mailToggle()
	tell application "System Events" to set runningProcesses to name of every process
	if runningProcesses contains "Mail" then
		tell application "Mail" to set enabled of account mailName to not (enabled of account mailName)
	end if
end mailToggle

on iChatToggle()
	tell application "System Events" to set runningProcesses to name of every process
	if runningProcesses contains "iChat" then
		tell application "iChat"
			set enabled of service ichatName to not (enabled of service ichatName)
			if enabled of service ichatName is true then
				log in service ichatName
			else
				log out service ichatName
			end if
		end tell
	end if
end iChatToggle

on colloquyToggle()
	tell application "System Events" to set runningProcesses to name of every process
	if runningProcesses contains "Colloquy" then
		tell application "Colloquy"
			if status of (every connection whose server address is colloquyName) contains disconnected then
				connect (every connection whose server address is colloquyName)
			else
				disconnect (every connection whose server address is colloquyName)
			end if
		end tell
	end if
end colloquyToggle